package com.qf.java2105.huangchuang.hotel.controller.food;

import com.alibaba.druid.util.StringUtils;
import com.qf.java2105.huangchuang.hotel.entity.ResultVO;
import com.qf.java2105.huangchuang.hotel.factory.BeanFactory;
import com.qf.java2105.huangchuang.hotel.pojo.Food;
import com.qf.java2105.huangchuang.hotel.pojo.FoodType;
import com.qf.java2105.huangchuang.hotel.service.IFoodService;
import com.qf.java2105.huangchuang.hotel.service.IFoodTypeService;
import com.qf.java2105.huangchuang.hotel.service.impl.FoodServiceImpl;
import com.qf.java2105.huangchuang.hotel.service.impl.FoodTypeServiceImpl;
import com.qf.java2105.huangchuang.hotel.utils.UploadFileUtils;
import org.apache.commons.beanutils.BeanUtils;

import javax.servlet.ServletException;
import javax.servlet.annotation.MultipartConfig;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;
import java.util.Map;

/**
 * 菜品更新页面控制
 * @author hcvg
 * @version v1.0
 * @date 2021/09/12
 **/
@WebServlet("/food/updateui")
@MultipartConfig
public class FoodUpdateUiController extends HttpServlet {
    IFoodService foodService  = (IFoodService) BeanFactory.getBean("foodService");
    IFoodTypeService foodTypeService = (IFoodTypeService) BeanFactory.getBean("foodTypeService");
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // 查询菜系列表
        ResultVO<List<FoodType>> resultVO = foodTypeService.findByFoodTypeName(null);
        request.setAttribute("foodTypes", resultVO.getData());
        // 通过回传的菜品id查询此菜品
        String idStr = request.getParameter("id");
        if (!StringUtils.isEmpty(idStr)){
            ResultVO<Food> foodResultVO = foodService.findById(Integer.valueOf(idStr));
            request.setAttribute("food", foodResultVO.getData());
        }
        request.getRequestDispatcher(request.getContextPath() + "/backend/detail/food/food-update.jsp").forward(request, response);
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        this.doGet(request, response);
    }
}
